﻿Imports System.Data.SqlClient
Imports System.IO

Public Class Etiquettes

    Private user As String
    Private userSite As String
    Private cn As CnData
    Private da As SqlDataAdapter
    Private cmd As SqlCommand
    Private dtr As SqlDataReader
    Private InfoSpe As Boolean
    Private TabZR As String
    Private spetype As String
    Private typeinfo As String


    Public Sub New(ByVal user As String, ByRef cn As CnData, ByVal userSite As String)
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Me.user = user
        Me.cn = cn
        Me.userSite = userSite

    End Sub

    Private Sub Etiquettes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        cn.Close()
    End Sub


    Private Sub Etiquettes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn.Open()
        cmd = New SqlCommand("SELECT NomDoc FROM Sites WHERE NumSite='" & userSite & "'", cn.getCn)
        SiteE.Text = cmd.ExecuteScalar
        da = New SqlDataAdapter("SELECT * FROM Sites WHERE Actif = 1", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "SitesD")
        With SiteD
            .DataSource = ds.Tables("SitesD")
            .DisplayMember = "NomDoc"
            .ValueMember = "NumSite"
            .SelectedIndex = 0
        End With
        da.Fill(ds, "SitesT")
        With SiteT
            .DataSource = ds.Tables("SitesT")
            .DisplayMember = "NomDoc"
            .ValueMember = "NumSite"
            .SelectedIndex = 0
        End With
        da = New SqlDataAdapter("SELECT * FROM Agres", cn.getCn)
        da.Fill(ds, "Agres")
        With C1
            .DataSource = ds.Tables("Agres")
            .DisplayMember = "NomAgres"
            .ValueMember = "NomAgres"
        End With
        da = New SqlDataAdapter("SELECT * FROM Flux WHERE Actif = 1 ORDER BY NomFlux", cn.getCn)
        da.Fill(ds, "Flux")
        With TypeF
            .DataSource = ds.Tables("Flux")
            .DisplayMember = "NomFlux"
            .ValueMember = "NumFlux"
            .SelectedIndex = 0
        End With

        cmd = New SqlCommand("SELECT * from Flux WHERE NumFlux='" & TypeF.SelectedValue & "'", cn.getCn)
        'val = cmd.ExecuteScalar
        dtr = cmd.ExecuteReader()
        dtr.Read()
        TabZR = dtr("TypeFlux")
        If dtr("InfoSpe") = "O" Then
            InfoSpe = True
            SpeTab.Visible = True
            SpeLabel.Text = dtr("TypeSpe") & " - " & dtr("DescSpe")
            spetype = dtr("TypeSpe").ToString
        Else
            InfoSpe = False
            SpeTab.Visible = False
            spetype = ""
        End If
        dtr.Close()

        da = New SqlDataAdapter("SELECT * FROM ZR WHERE TypeFLux='" & TabZR & "' AND (Site is null OR Site='" & SiteD.SelectedValue & "') AND Actif = 1", cn.getCn)
        If da.Fill(ds, "ZR") > 0 Then
            recherche.Enabled = True
            recherche.Text = Nothing
            Dim tmp As String = ds.Tables("ZR").Rows(0).Item("Choix").ToString
            typeinfo = tmp
            With InfoC
                .DataSource = ds.Tables("ZR")
                If ds.Tables("ZR").Rows(0).Item("Choix").ToString = "Rayon" Then
                    .DisplayMember = "Rayon" 'ds.Tables("ZR").Columns(0).ColumnName
                    .ValueMember = "IdZR"
                    InfoLabel.Text = "Rayon"
                Else
                    If tmp = "INFO1" Then
                        InfoC.DisplayMember = "INFO1_Doc"
                    Else
                        InfoC.DisplayMember = "INFO2_Doc"
                    End If
                    .ValueMember = "IdZR"
                    InfoLabel.Text = ds.Tables("ZR").Rows(0).Item(ds.Tables("ZR").Rows(0).Item("Choix").ToString).ToString
                End If
                InfoC.Refresh()
            End With

            If InfoLabel.Text = "Rayon" Then
                CompLabel.Text = ds.Tables("ZR").Rows(0).Item("INFO1").ToString
                ZR.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("ZR")
                CompVal.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("INFO1_Eti")
            Else
                CompLabel.Text = "Rayon"
                ZR.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("ZR")
                CompVal.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("Rayon")
            End If

        End If
        cn.Close()

    End Sub

    Private Sub Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fermer.Click
        Me.Close()
    End Sub

    Private Sub transit_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles transit.CheckedChanged
        If transit.Checked Then
            SiteT.Visible = True
        Else
            SiteT.Visible = False
        End If
    End Sub

    Private Sub TypeF_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeF.SelectedIndexChanged
        SpeListe.Items.Clear()
        If TypeF.ValueMember = "" Then
            InfoLabel.Text = "Information destinataire"
            CompLabel.Text = ""
            ZR.Text = ""
            CompVal.Text = ""
            Exit Sub
        End If

        cn.Open()
        cmd = New SqlCommand("SELECT * from Flux WHERE NumFlux='" & TypeF.SelectedValue & "'", cn.getCn)
        'val = cmd.ExecuteScalar
        dtr = cmd.ExecuteReader()
        dtr.Read()
        TabZR = dtr("TypeFlux")
        If dtr("InfoSpe") = "O" Then
            InfoSpe = True
            SpeTab.Visible = True
            SpeLabel.Text = dtr("TypeSpe") & " - " & dtr("DescSpe")
            spetype = dtr("TypeSpe").ToString
        Else
            InfoSpe = False
            SpeTab.Visible = False
            spetype = ""
        End If
        dtr.Close()

        'da = New SqlDataAdapter("SELECT * FROM ZR_" & TabZR, cn.getCn)
        'Try
        ' ds.Tables("ZR_" & TabZR).Clear()
        ' Catch ex As Exception
        '
        '        End Try
        '        da.Fill(ds, "ZR_" & TabZR)
        '       InfoLabel.Text = ds.Tables("ZR_" & TabZR).Columns(0).ColumnName
        '        CompLabel.Text = ds.Tables("ZR_" & TabZR).Columns(2).ColumnName
        ''       With InfoC
        ' .DataSource = ds.Tables("ZR_" & TabZR)
        ' .DisplayMember = ds.Tables("ZR_" & TabZR).Columns(0).ColumnName
        ' .ValueMember = ds.Tables("ZR_" & TabZR).Columns(0).ColumnName
        '.SelectedIndex = 0
        'End With

        da = New SqlDataAdapter("SELECT * FROM ZR WHERE TypeFLux='" & TabZR & "' AND (Site is null OR Site='" & SiteD.SelectedValue & "') AND Actif = 1", cn.getCn)
        Try
            ds.Tables("ZR").Clear()
        Catch ex As Exception

        End Try
        If da.Fill(ds, "ZR") > 0 Then
            recherche.Enabled = True
            recherche.Text = Nothing
            Dim tmp As String = ds.Tables("ZR").Rows(0).Item("Choix").ToString
            With InfoC
                .DataSource = ds.Tables("ZR")
                If ds.Tables("ZR").Rows(0).Item("Choix").ToString = "Rayon" Then
                    .DisplayMember = "Rayon" 'ds.Tables("ZR").Columns(0).ColumnName
                    .ValueMember = "IdZR"
                    InfoLabel.Text = "Rayon"
                Else
                    If tmp = "INFO1" Then
                        InfoC.DisplayMember = "INFO1_Doc"
                    Else
                        InfoC.DisplayMember = "INFO2_Doc"
                    End If
                    .ValueMember = "IdZR"
                    InfoLabel.Text = ds.Tables("ZR").Rows(0).Item(ds.Tables("ZR").Rows(0).Item("Choix").ToString).ToString
                End If
                InfoC.Refresh()
            End With

            If InfoLabel.Text = "Rayon" Then
                CompLabel.Text = ds.Tables("ZR").Rows(0).Item("INFO1").ToString
                ZR.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("ZR")
                CompVal.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("INFO1_Eti")
            Else
                CompLabel.Text = "Rayon"
                ZR.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("ZR")
                CompVal.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("Rayon")
            End If
        Else
            recherche.Enabled = False
            recherche.Text = Nothing
            InfoLabel.Text = "Information destinataire"
            CompLabel.Text = ""
            ZR.Text = ""
            CompVal.Text = ""

        End If
        cn.Close()

        'If TypeOf InfoC.SelectedValue Is String Then
        'Dim dr As DataRow()
        'dr = ds.Tables("ZR_" & TabZR).Select(InfoLabel.Text & "='" & InfoC.SelectedValue & "'")
        'If dr.Length > 0 Then
        ' ZR.Text = dr(0).Item(1).ToString()
        ' CompVal.Text = dr(0).Item(2).ToString()
        ' Else
        ' ZR.Text = ""
        ' CompVal.Text = ""
        ' End If
        ' End If
    End Sub

    Private Sub SpeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SpeAdd.Click
        If IsNothing(SpeNum) Or String.IsNullOrEmpty(SpeNum.Text) Then Exit Sub
        If Not SpeListe.Items.Contains(SpeNum.Text) Then SpeListe.Items.Add(SpeNum.Text)
        SpeNum.Text = Nothing
    End Sub

    Private Sub SpeNum_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SpeNum.KeyDown
        If e.KeyCode = Keys.Enter Then
            SpeAdd.PerformClick()
        End If
    End Sub



    Private Sub InfoC_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles InfoC.SelectedValueChanged
        If InfoC.ValueMember = "" Then
            ZR.Text = ""
            CompVal.Text = ""
            Exit Sub
        End If
        If InfoC.DisplayMember = "" Then
            ZR.Text = ""
            CompVal.Text = ""
            Exit Sub
        End If
        If String.IsNullOrEmpty(InfoC.Text) Then
            ZR.Text = ""
            CompVal.Text = ""
            Exit Sub
        End If
        If String.IsNullOrEmpty(InfoC.SelectedValue) Then
            ZR.Text = ""
            CompVal.Text = ""
            Exit Sub
        End If

        If InfoLabel.Text = "Rayon" Then
            'CompLabel.Text = ds.Tables("ZR").Rows(0).Item("INFO1").ToString
            ZR.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("ZR")
            CompVal.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("INFO1_Eti")
        Else
            'CompLabel.Text = "Rayon"
            ZR.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("ZR")
            CompVal.Text = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0).Item("Rayon")
        End If
    End Sub



    Private Sub ListeE_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ListeE.CellEndEdit
        If ListeE.Rows(e.RowIndex).Cells(0).Value = "Palette" Or ListeE.Rows(e.RowIndex).Cells(0).Value = "Roulant" Or ListeE.Rows(e.RowIndex).Cells(0).Value = "Roll" Or ListeE.Rows(e.RowIndex).Cells(0).Value = "Ridelle" Or ListeE.Rows(e.RowIndex).Cells(0).Value = "Portant" Or ListeE.Rows(e.RowIndex).Cells(0).Value = "Socle" Then
            ListeE.Rows(e.RowIndex).Cells(1).Value = 1
            ListeE.Rows(e.RowIndex).Cells(1).ReadOnly = True
        ElseIf e.RowIndex >= 0 Then
            ListeE.Rows(e.RowIndex).Cells(1).ReadOnly = False
        End If
        If (e.ColumnIndex = 1 Or e.ColumnIndex = 2) And Not IsNumeric(ListeE.Item(e.ColumnIndex, e.RowIndex).Value) And Not String.IsNullOrEmpty(ListeE.Item(e.ColumnIndex, e.RowIndex).Value) Then
            MsgBox("Veuiller saisir une valeur numerique")
            ListeE.Item(e.ColumnIndex, e.RowIndex).Value = Nothing
        ElseIf e.ColumnIndex = 2 Then
            If ListeE.Item(e.ColumnIndex, e.RowIndex).Value > 1 Then ListeE.Item(1, e.RowIndex).Value = 1
            NbEti.Text = 0
            For Each drow As DataGridViewRow In ListeE.Rows
                If Not drow.IsNewRow Then
                    If String.IsNullOrEmpty(drow.Cells(2).Value) Then
                        drow.Cells(2).Value = 1
                        NbEti.Text = CInt(NbEti.Text) + 1
                    Else
                        NbEti.Text = CInt(NbEti.Text) + CInt(drow.Cells(2).Value)
                    End If
                End If
            Next
            If CInt(NbEti.Text) > 150 Then
                MsgBox("Vous avez dépassé le nombre maximum d'étiquette")
                ListeE.Item(e.ColumnIndex, e.RowIndex).Value = Nothing
                NbEti.Text = 0
                For Each drow As DataGridViewRow In ListeE.Rows
                    If Not drow.IsNewRow Then
                        If String.IsNullOrEmpty(drow.Cells(2).Value) Then
                            drow.Cells(2).Value = 0
                            NbEti.Text = CInt(NbEti.Text) + 1
                        Else
                            NbEti.Text = CInt(NbEti.Text) + CInt(drow.Cells(2).Value)
                        End If
                    End If
                Next
            End If
        ElseIf e.ColumnIndex = 1 Then
            If ListeE.Item(e.ColumnIndex, e.RowIndex).Value > 1 Then ListeE.Item(2, e.RowIndex).Value = 1
            NbEti.Text = 0
            For Each drow As DataGridViewRow In ListeE.Rows
                If Not drow.IsNewRow Then
                    If String.IsNullOrEmpty(drow.Cells(2).Value) Then
                        drow.Cells(2).Value = 1
                        NbEti.Text = CInt(NbEti.Text) + 1
                    Else
                        NbEti.Text = CInt(NbEti.Text) + CInt(drow.Cells(2).Value)
                    End If
                End If
            Next
            If CInt(NbEti.Text) > 150 Then
                MsgBox("Vous avez dépassé le nombre maximum d'étiquette")
                ListeE.Item(2, e.RowIndex).Value = Nothing
                NbEti.Text = 0
                For Each drow As DataGridViewRow In ListeE.Rows
                    If Not drow.IsNewRow Then
                        If String.IsNullOrEmpty(drow.Cells(2).Value) Then
                            drow.Cells(2).Value = 0
                            NbEti.Text = CInt(NbEti.Text) + 1
                        Else
                            NbEti.Text = CInt(NbEti.Text) + CInt(drow.Cells(2).Value)
                        End If
                    End If
                Next
            End If
        End If
    End Sub


    Private Sub ListeE_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles ListeE.RowHeaderMouseDoubleClick
        If e.RowIndex >= 0 And e.RowIndex < ListeE.RowCount - 1 Then
            ListeE.Rows.RemoveAt(e.RowIndex)
        End If
    End Sub

    Private Sub Imprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Imprimer.Click

        If String.IsNullOrEmpty(My.Settings.printerType) Or String.IsNullOrEmpty(My.Settings.printer) Then
            MsgBox("Veuillez selectionner l'imprimante")
            Exit Sub
        End If
        For Each drow As DataGridViewRow In ListeE.Rows
            If Not drow.IsNewRow Then
                If String.IsNullOrEmpty(drow.Cells(0).Value) Then
                    MsgBox("Veuillez indiquer le type d'agrès pour les étiquettes demandées")
                    Exit Sub
                End If
            End If
        Next
        If String.IsNullOrEmpty(InfoC.Text) Then
            MsgBox("Veuillez sélectionner l'information destinataire")
            Exit Sub
        End If
        Dim compteur As Integer
        Dim num As String

        cn.Open()
        cmd = New SqlCommand("SELECT Compteur FROM Flux WHERE NumFlux='" & TypeF.SelectedValue & "'", cn.getCn)
        compteur = cmd.ExecuteScalar()
        cmd = New SqlCommand("UPDATE Flux SET Compteur=Compteur+" & CInt(NbEti.Text) & " WHERE NumFlux='" & TypeF.SelectedValue & "'", cn.getCn)
        cmd.ExecuteNonQuery()
        Dim sumagres As Integer = 0
        For Each drow As DataGridViewRow In ListeE.Rows
            If Not drow.IsNewRow Then
                If String.IsNullOrEmpty(drow.Cells(2).Value) Then

                Else
                    sumagres = sumagres + CInt(drow.Cells(2).Value)
                End If
            End If
        Next
        Dim temp = Environment.GetEnvironmentVariable("Temp")
        Dim s As StreamWriter
        s = New StreamWriter(New FileStream(temp & "\Imprimer.txt", FileMode.Create, FileAccess.Write))
        Dim speval As String
        Dim compteurEti As Integer = 0
        Dim dr As DataRow
        dr = ds.Tables("ZR").Select(InfoC.DisplayMember & "='" & InfoC.Text & "'")(0)
        If My.Settings.printerType = "PAXAR MONARCH" Then
            s.WriteLine("{")
            s.WriteLine("I,")
            s.WriteLine("A,,,0,0,1|")
            s.WriteLine("B,,,,0,|")
            s.WriteLine("C,,0,,0,|")
            s.WriteLine("G,0,,|")
            s.WriteLine("E,""~123~044~034~124~125~126~094""|")
            s.WriteLine("}")
        End If
        For Each drow As DataGridViewRow In ListeE.Rows
            If Not drow.IsNewRow Then
                For i = 1 To CInt(drow.Cells(2).Value)
                    compteur = CInt(compteur) + 1
                    num = userSite + TypeF.SelectedValue + SiteD.SelectedValue + Format(Date.Today, "ddMMyy") + CStr(("0000" + CStr(compteur))).Substring((CStr(("0000" + CStr(compteur))).Length) - 4, 4)
                    num = num + ("0" & CStr(cle(num))).Substring(("0" & CStr(cle(num))).Length - 2, 2)
                    'num = num + ("0" & CStr(cle(num)))  
                    sumagres = 1
                    compteurEti = compteurEti + 1
                    If Not String.IsNullOrEmpty(drow.Cells(1).Value) Then sumagres = drow.Cells(1).Value
                    If transit.Checked Then
                        cmd = New SqlCommand("INSERT INTO LP (LP,Expediteur,Destinataire,Transitaire,Flux,TypeAgres,NombreColis,DateImp,IdZR,UtilImp,Statut) VALUES ('" & num & "','" & userSite & "','" & SiteD.SelectedValue & "','" & SiteT.SelectedValue & "','" & TypeF.SelectedValue & "','" & drow.Cells(0).Value & "'," & sumagres & ",'" & Date.Today & "'," & InfoC.SelectedValue & ",'" & user & "','Créé')", cn.getCn)
                    Else
                        cmd = New SqlCommand("INSERT INTO LP (LP,Expediteur,Destinataire,Flux,TypeAgres,NombreColis,DateImp,IdZR,UtilImp,Statut) VALUES ('" & num & "','" & userSite & "','" & SiteD.SelectedValue & "','" & TypeF.SelectedValue & "','" & drow.Cells(0).Value & "'," & sumagres & ",'" & Date.Today & "'," & InfoC.SelectedValue & ",'" & user & "','Créé')", cn.getCn)
                    End If
                    cmd.ExecuteNonQuery()
                    'MsgBox(cmd.CommandText)
                    'Exit Sub
                    speval = ""
                    If InfoSpe Then
                        For j = 0 To SpeListe.Items.Count - 1
                            If j > 0 Then
                                speval = SpeListe.Items(j).ToString & "*"
                            Else
                                speval = SpeListe.Items(j).ToString
                            End If

                            cmd = New SqlCommand("INSERT INTO SpeLP (LP,Spe) VALUES ('" & num & "','" & SpeListe.Items(j).ToString & "')", cn.getCn)
                            cmd.ExecuteNonQuery()
                        Next
                    End If
                    If My.Settings.printerType = "PAXAR MONARCH" Then
                        s.WriteLine("{")
                        s.WriteLine("F,999,A,R,G,610,812,""FSSS999""|")
                        s.WriteLine("B,1,18,V,472,660,8,6,78,8,L,2|")
                        s.WriteLine("R,1,""" & num & """|")
                        s.WriteLine("C,501,617,0,17,1,1,O,L,0,2,""" & num & """,1|")
                        s.WriteLine("L,V,513,60,0,751,3,""""|")
                        s.WriteLine("L,V,515,667,90,65,3,""""|")
                        s.WriteLine("C,549,188,0,17,1,1,O,L,0,2,""" & compteurEti & "/" & NbEti.Text & """,1|")
                        s.WriteLine("C,549,771,0,17,1,1,O,L,0,2,""" & spetype & """,1|")
                        s.WriteLine("C,548,651,0,17,1,1,O,L,0,2,""" & speval & """,1|")
                        s.WriteLine("C,77,771,0,17,2,1,O,L,0,2,""" & TypeF.Text & """,1|")
                        s.WriteLine("L,V,87,60,0,751,3,""""|")
                        s.WriteLine("L,V,391,60,0,751,3,""""|")
                        s.WriteLine("C,132,332,0,1,2,1,O,L,0,2,""DATE:"",1|")
                        s.WriteLine("C,188,331,0,1,2,1,O,L,0,2,""RAYON:"",1|")
                        s.WriteLine("C,244,611,0,1,2,1,O,L,0,2,""Zone de roulage:"",1|")
                        s.WriteLine("C,308,611,0,1,2,1,O,L,0,2,""" & dr("INFO1").ToString & ":"",1|")
                        s.WriteLine("C,372,611,0,1,2,1,O,L,0,2,""" & dr("INFO2").ToString & ":"",1|")
                        s.WriteLine("L,V,327,60,0,559,3,""""|")
                        s.WriteLine("L,V,263,60,0,559,3,""""|")
                        s.WriteLine("C,132,244,0,1,2,1,O,L,0,2,""" & Date.Today & """,1|")
                        s.WriteLine("C,188,220,0,1,2,1,O,L,0,2,""" & dr("Rayon").ToString & """,1|")
                        s.WriteLine("C,260,771,0,1,2,1,O,L,0,2,""" & drow.Cells(0).Value & """,1|")
                        s.WriteLine("L,V,143,60,0,751,3,""""|")
                        s.WriteLine("L,V,199,60,0,751,3,""""|")
                        s.WriteLine("L,V,90,355,90,112,3,""""|")
                        s.WriteLine("C,132,771,0,1,2,1,O,L,0,2,""DE:"",1|")
                        s.WriteLine("C,188,771,0,1,2,1,O,L,0,2,""VERS:"",1|")
                        s.WriteLine("C,132,715,0,1,2,1,O,L,0,2,""" & ds.Tables("SitesD").Select("NumSite=" & userSite)(0).Item("NomEti").ToString & """,1|")
                        s.WriteLine("C,188,683,0,1,2,1,O,L,0,2,""" & ds.Tables("SitesD").Select("NumSite=" & SiteD.SelectedValue)(0).Item("NomEti").ToString & """,1|")
                        s.WriteLine("L,V,198,619,90,196,3,""""|")
                        s.WriteLine("C,356,771,0,1,2,1,O,L,0,2,""" & sumagres & """,1|")
                        s.WriteLine("L,V,296,619,0,192,2,""""|")
                        s.WriteLine("C,372,451,0,1,2,1,O,L,0,2,""" & dr("INFO2_Eti").ToString & """,1|")
                        s.WriteLine("C,308,499,0,1,2,1,O,L,0,2,""" & dr("INFO1_Eti").ToString & """,1|")
                        s.WriteLine("C,244,395,0,1,2,1,O,L,0,2,""" & dr("ZR").ToString & """,1|")
                        s.WriteLine("C,554,23,0,1,1,1,O,L,0,3,""" & num & """,1|")
                        s.WriteLine("C,259,23,0,1,1,1,O,L,0,3,""" & num & """,1|")
                        s.WriteLine("}")
                        s.WriteLine("{")
                        s.WriteLine("B,999,N,1|")
                        s.WriteLine("E,,,,1|")
                        s.WriteLine("}")
                    Else
                        s.WriteLine("{D0782,1016,0762|}")
                        s.WriteLine("{C|}")
                        s.WriteLine("{XB00;0200,0490,A,3,03,0,0128,+0000000000,000,0,00|}")
                        s.WriteLine("{RB00;>5" & num & "|}")
                        s.WriteLine("{PV00;0260,0656,0042,0042,H,00,B=" & num & "|}")
                        s.WriteLine("{LC;0000,0668,0910,0668,0,3|}")
                        s.WriteLine("{PV01;0030,0715,0042,0042,H,00,B=" & spetype & "|}")
                        s.WriteLine("{LC;0189,0671,0189,0761,0,3|}")
                        s.WriteLine("{LC;0718,0671,0718,0761,0,3|}")
                        s.WriteLine("{PV02;0220,0716,0042,0042,H,00,B=" & speval & "|}")
                        s.WriteLine("{PV03;0740,0715,0042,0042,H,00,B=" & compteurEti & "/" & NbEti.Text & "|}")
                        s.WriteLine("{PV04;0979,0730,0035,0035,H,33,B=" & num & "|}")
                        s.WriteLine("{PV05;0979,0350,0035,0035,H,33,B=" & num & "|}")
                        s.WriteLine("{LC;0000,0469,0910,0469,0,3|}")
                        s.WriteLine("{PV06;0470,0449,0028,0056,H,00,B=" & dr("INFO2_Eti").ToString & "|}")
                        s.WriteLine("{PV07;0310,0449,0028,0056,H,00,B=" & dr("INFO2").ToString & ":|}")
                        s.WriteLine("{PV08;0420,0379,0028,0056,H,00,B=" & dr("INFO1_Eti").ToString & "|}")
                        s.WriteLine("{PV09;0310,0379,0028,0056,H,00,B=" & dr("INFO1").ToString & ":|}")
                        s.WriteLine("{PV10;0520,0309,0028,0056,H,00,B=" & dr("ZR").ToString & "|}")
                        s.WriteLine("{PV11;0310,0309,0028,0056,H,00,B=Zone de roulage:|}")
                        s.WriteLine("{LC;0000,0259,0910,0259,0,3|}")
                        s.WriteLine("{PV12;0520,0239,0028,0056,H,00,B=RAYON:|}")
                        s.WriteLine("{PV13;0030,0239,0028,0056,H,00,B=VERS:|}")
                        s.WriteLine("{PV14;0130,0239,0028,0056,H,00,B=" & ds.Tables("SitesD").Select("NumSite=" & SiteD.SelectedValue)(0).Item("NomEti").ToString & "|}")
                        s.WriteLine("{PV15;0030,0169,0028,0056,H,00,B=DE:|}")
                        s.WriteLine("{PV16;0090,0169,0028,0056,H,00,B=" & ds.Tables("SitesD").Select("NumSite=" & userSite)(0).Item("NomEti").ToString & "|}")
                        s.WriteLine("{PV17;0640,0239,0028,0056,H,00,B=" & dr("Rayon").ToString & "|}")
                        s.WriteLine("{PV18;0520,0169,0028,0056,H,00,B=DATE:|}")
                        s.WriteLine("{PV19;0620,0169,0028,0056,H,00,B=" & Date.Today & "|}")
                        s.WriteLine("{LC;0000,0189,0910,0189,0,3|}")
                        s.WriteLine("{LC;0000,0119,0910,0119,0,3|}")
                        s.WriteLine("{LC;0499,0120,0499,0260,0,3|}")
                        s.WriteLine("{LC;0299,0255,0299,0465,0,3|}")
                        s.WriteLine("{PV20;0030,0086,0063,0063,H,00,B=" & TypeF.Text & "|}")
                        s.WriteLine("{LC;0300,0399,0914,0399,0,3|}")
                        s.WriteLine("{LC;0300,0329,0914,0329,0,3|}")
                        s.WriteLine("{LC;0000,0359,0300,0359,0,3|}")
                        s.WriteLine("{PV21;0030,0324,0049,0049,H,00,B=" & drow.Cells(0).Value & "|}")
                        s.WriteLine("{PV22;0030,0424,0049,0049,H,00,B=" & sumagres & "|}")
                        s.WriteLine("{XS;I,0001,0002C6111|}")
                    End If
                Next
                'ListeE.Rows.RemoveAt(drow.Index)

            End If
        Next
        s.Close()
        cn.Close()
        RawPrinterHelper.SendFileToPrinter(My.Settings.printer, temp & "\Imprimer.txt")
        'ListeE.DataSource = Nothing
        While ListeE.Rows.Count > 1
            ListeE.Rows.RemoveAt(0)
        End While
        SpeListe.Items.Clear()
        NbEti.Text = 0
    End Sub


    Private Function cle(ByVal num As String) As String
        Return ((CInt(num.Substring(0, 2)) + CInt(num.Substring(2, 2)) + CInt(num.Substring(4, 2)) + CInt(num.Substring(6, 2)) + CInt(num.Substring(8, 2)) + CInt(num.Substring(10, 2)) + CInt(num.Substring(12, 2)) + CInt(num.Substring(14, 2))) Mod 100)
    End Function

    Private Sub SiteD_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SiteD.SelectedIndexChanged
        TypeF_SelectedIndexChanged(Me, e)
    End Sub

    Private Sub ListeE_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles ListeE.RowsRemoved
        NbEti.Text = 0
        For Each drow As DataGridViewRow In ListeE.Rows
            If Not drow.IsNewRow Then
                If String.IsNullOrEmpty(drow.Cells(2).Value) Then
                    drow.Cells(2).Value = 1
                    NbEti.Text = CInt(NbEti.Text) + 1
                Else
                    NbEti.Text = CInt(NbEti.Text) + CInt(drow.Cells(2).Value)
                End If
            End If
        Next
    End Sub


    Private Sub recherche_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recherche.TextChanged
        Dim retrdvview As New DataView
        Dim a, b As String
        a = InfoC.DisplayMember
        b = InfoC.ValueMember
        With retrdvview
            .Table = ds.Tables("ZR")
            .RowFilter = InfoC.DisplayMember & " like '%" & recherche.Text & "%'"
        End With
        InfoC.ValueMember = ""
        InfoC.DataSource = retrdvview.ToTable()
        InfoC.DroppedDown = True
        Me.Cursor = Cursors.Default
        InfoC.DisplayMember = a
        InfoC.ValueMember = b
    End Sub

    Private Sub recherche_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles recherche.KeyPress
        If e.KeyChar = Chr(34) Or e.KeyChar = "'" Or e.KeyChar = "*" Then
            e.KeyChar = ""
            Exit Sub
        End If
    End Sub

    Private Sub SpeListe_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SpeListe.MouseDoubleClick
        On Error Resume Next
        SpeListe.Items.RemoveAt(SpeListe.SelectedIndex)
    End Sub


    Private Sub ListeE_CellValueChange(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs)
        If e.RowIndex < 0 Then Exit Sub
        MsgBox(e.ToString)
        If e.ColumnIndex = 0 Then
            If e.ToString = "Palette" Or e.ToString = "Roulant" Or e.ToString = "Roll" Or e.ToString = "Ridelle" Or e.ToString = "Portant" Or e.ToString = "Socle" Then
                ListeE.Rows(e.RowIndex).Cells(1).Value = "1"
                ListeE.Rows(e.RowIndex).Cells(1).ReadOnly = True
            Else
                ListeE.Rows(e.RowIndex).Cells(1).ReadOnly = False
            End If
        End If
    End Sub


    Private Sub ListeE_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ListeE.CellContentClick

    End Sub
End Class
